package com.ihd.ihardware.view.tzc.me.model;

import android.os.Bundle;
import android.text.TextUtils;

import com.google.gson.Gson;
import com.google.gson.JsonObject;
import com.google.gson.reflect.TypeToken;
import com.ihd.ihardware.api.HttpClient;
import com.ihd.ihardware.common.AppConstans;
import com.ihd.ihardware.pojo.DeviceListRes;
import com.ihd.ihardware.pojo.EmptyRes;
import com.ihd.ihardware.pojo.FileRes;
import com.ihd.ihardware.pojo.OtherUserInfoRes;
import com.ihd.ihardware.pojo.SignsRes;
import com.ihd.ihardware.pojo.UserInfoRes;

import org.reactivestreams.Publisher;

import java.io.File;
import java.util.List;

import cn.wowjoy.commonlibrary.http.BaseSubscriber;
import cn.wowjoy.commonlibrary.rxbus.RxBus;
import cn.wowjoy.commonlibrary.utils.RxSchedulerTransformer;
import cn.wowjoy.commonlibrary.utils.SPUtils;
import cn.wowjoy.commonlibrary.viewmodel.BaseRepository;
import io.reactivex.Flowable;
import io.reactivex.functions.Consumer;
import io.reactivex.functions.Function;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class MeRepository extends BaseRepository {
    public void updateHead(File file) {
        MultipartBody.Part requestBody = MultipartBody.Part.createFormData("file", file.getName(), RequestBody.create(MediaType.parse("image/*"), file));
        addDisposable(HttpClient.getService().uploadToOss(requestBody)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<FileRes>(AppConstans.UPDATE) {
                }));
    }

    public void updateInfo(String avatar, String nickName, int sex, String birthday, String height, String weight) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("avatar", avatar);
        mParam.addProperty("nickName", nickName);
        mParam.addProperty("sex", sex);
        mParam.addProperty("birthday", birthday);
        mParam.addProperty("height", height);
        mParam.addProperty("weight", weight);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().userInfo(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.UPDATEINFO) {
                }));
    }

    public void dream(String weight) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("weight", weight);
        mParam.addProperty("weightPeriod", "0");
        mParam.addProperty("stepNumber", "0");
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().dream(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.DREAM) {
                }));
    }

    public void dream(String weight, String weightPeriod, String stepNum) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("weight", weight);
        mParam.addProperty("weightPeriod", weightPeriod);
        mParam.addProperty("stepNumber", stepNum);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().dream(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<EmptyRes>(AppConstans.DREAM) {
                }));
    }

    public void getUserInfo() {
        addDisposable(HttpClient.getService().deviceList(1, 100)
                .flatMap(new Function<DeviceListRes, Publisher<UserInfoRes>>() {
                    @Override
                    public Publisher<UserInfoRes> apply(DeviceListRes deviceListRes) throws Exception {
                        if (deviceListRes.getData().getList().size() == 0)
                            SPUtils.putString(AppConstans.DEVICES, "");
                        else
                            SPUtils.putString(AppConstans.DEVICES, new Gson().toJson(deviceListRes.getData().getList(), new TypeToken<List<DeviceListRes.DataBean.ListBean>>() {
                            }.getType()));
                        return HttpClient.getService().getUserInfo();
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<UserInfoRes>(AppConstans.INFO) {
                }));
    }

    public void getOtherUserInfo(String userId) {
        addDisposable(HttpClient.getService().otherUserInfo(userId)
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<OtherUserInfoRes>(AppConstans.INFOS) {
                }));
    }

    public void getSigns(String userId, int pageNum, int pageSize) {
        if (TextUtils.isEmpty(userId))
            addDisposable(HttpClient.getService().getSigns(pageNum, pageSize)
                    .compose(new RxSchedulerTransformer<>())
                    .subscribeWith(new BaseSubscriber<SignsRes>(AppConstans.PAGE) {
                    }));
        else
            addDisposable(HttpClient.getService().otherUserSigns(userId, pageNum, pageSize)
                    .compose(new RxSchedulerTransformer<>())
                    .subscribeWith(new BaseSubscriber<SignsRes>(AppConstans.PAGE) {
                    }));
    }

    public void concern(String concernUserId) {
        JsonObject mParam = new JsonObject();
        mParam.addProperty("concernUserId", concernUserId);
        RequestBody rb = RequestBody.create(MediaType.parse("application/json;charset=UTF-8"), mParam.toString());
        addDisposable(HttpClient.getService().concern(rb)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("type", 1);
                        RxBus.getInstance().post(AppConstans.CONCERN_PAGE, b);
                    }
                }));
    }

    public void delConcern(String concernUserId) {
        addDisposable(HttpClient.getService().delConcern(concernUserId)
                .compose(new RxSchedulerTransformer<>())
                .subscribe(new Consumer<EmptyRes>() {
                    @Override
                    public void accept(EmptyRes emptyRes) throws Exception {
                        Bundle b = new Bundle();
                        b.putInt("type", 0);
                        RxBus.getInstance().post(AppConstans.CONCERN_PAGE, b);
                    }
                }));
    }

    public void unbind(String id) {
        addDisposable(HttpClient.getService().unbind(id)
                .flatMap(new Function<EmptyRes, Publisher<DeviceListRes>>() {
                    @Override
                    public Publisher<DeviceListRes> apply(EmptyRes emptyRes) throws Exception {
                        if (emptyRes.getCode() == 0)
                            return HttpClient.getService().deviceList(1, 100);
                        else {
                            return Flowable.error(new Exception(emptyRes.getMessage()));
                        }
                    }
                })
                .compose(new RxSchedulerTransformer<>())
                .subscribeWith(new BaseSubscriber<DeviceListRes>(AppConstans.UNBIND) {
                }));
    }
}
